#pragma once
/**
 * @file HttpResponse.hpp
 * @author 1102403684
 * @brief 封装http响应
 * @version 0.1
 * @date 2024-10-30
 *
 * @copyright Copyright (c) 2024
 *
 */

#include <string>
#include <unordered_map>

class HttpResponse
{
public:
    HttpResponse() : is_redirect_(false), statu_(200)
    {
    }

    HttpResponse(int statu) : is_redirect_(false), statu_(statu)
    {
    }

    void reset()
    {
        statu_ = 200;
        is_redirect_ = false;
        body_.clear();
        redirect_url_.clear();
        headers_.clear();
    }

    void set_header(const std::string &key, const std::string &val)
    {
        headers_.insert(std::make_pair(key, val));
    }
    std::string get_header(const std::string &key)
    {
        if (!has_header(key))
            return "";

        return headers_[key];
    }
    bool has_header(const std::string &key)
    {
        auto it = headers_.find(key);
        if (it == headers_.end())
        {
            return false;
        }
        return true;
    }

    void set_content(const std::string &body, const std::string &content_type)
    {
        body_ = body;
        set_header("Content-Type", content_type);
    }
    void set_redirect(const std::string &path, int statu = 302 /*临时重定向*/)
    {
        statu_ = statu;
        is_redirect_ = true;
        redirect_url_ = path;
    }

public:
    int statu_;
    std::unordered_map<std::string, std::string> headers_;
    std::string body_;
    bool is_redirect_;
    std::string redirect_url_;
};